package com.bocweb.fly.jiaodao.fragment;


import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bocweb.fly.jiaodao.R;
import com.bocweb.fly.jiaodao.bean.KeyValueBean;
import com.bocweb.fly.jiaodao.bean.PopBean;
import com.bocweb.fly.jiaodao.utils.expandpop.ExpandPopTabView;
import com.bocweb.fly.jiaodao.utils.expandpop.PopOneListView;
import com.milk.base.BaseFragment;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by fly on 16/10/10.
 */
public class FmFx extends BaseFragment implements ExpandPopTabView.tButtonInterface {
    private ArrayList<View> mViewArray = new ArrayList<View>();
    @Bind(R.id.expandtab_view)
    ExpandPopTabView expandtabView;
    private List<KeyValueBean> mPriceLists;
    private List<KeyValueBean> mSortLists;
    private List<KeyValueBean> mFavorLists=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
    @Override
    protected boolean flux() {
        return false;
    }

    @Override
    public void initData() {

        setConfigsDatas();
        addItem(expandtabView, mPriceLists, "", "全部");
        addItem(expandtabView, mFavorLists, "默认", "地区");
        addItem(expandtabView, mSortLists, "优惠最多", "行业");
        addItem(expandtabView, mSortLists, "优惠最多", "智能排序");

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(expandtabView != null){
            expandtabView.onExpandPopView();
        }
    }

    public void addItem(ExpandPopTabView expandTabView, List<KeyValueBean> lists, String defaultSelect, String defaultShowText) {
        PopOneListView popOneListView = new PopOneListView(getActivity());
        popOneListView.setDefaultSelectByValue(defaultSelect);
        //popViewOne.setDefaultSelectByKey(defaultSelect);
        popOneListView.setCallBackAndData(lists, expandTabView, new PopOneListView.OnSelectListener() {
            @Override
            public void getValue(String key, String value) {
                Log.e("tag", "key :" + key + " ,value :" + value);
            }
        });
        expandTabView.addItemToExpandTab(defaultShowText, popOneListView,this);
    }

    private void setConfigsDatas() {
        try {
            InputStream is = getActivity().getAssets().open("searchType");
            String searchTypeJson = readStream(is);
            PopBean messageDTO = JSONObject.parseObject(searchTypeJson, PopBean.class);
           // ConfigsDTO configsDTO = messageDTO.getInfo();

            mPriceLists = messageDTO.getInfo().getPriceType();
            mSortLists = messageDTO.getInfo().getSortType();
           // mFavorLists = messageDTO.getInfo().getSortType();

       /*     List<ConfigAreaDTO> configAreaListDTO = configsDTO.getCantonAndCircle();
            for (ConfigAreaDTO configAreaDTO : configAreaListDTO) {
                KeyValueBean keyValueBean = new KeyValueBean();
                keyValueBean.setKey(configAreaDTO.getKey());
                keyValueBean.setValue(configAreaDTO.getValue());
                mParentLists.add(keyValueBean);

                ArrayList<KeyValueBean> childrenLists = new ArrayList<>();
                for (KeyValueBean keyValueBean1 : configAreaDTO.getBusinessCircle()) {
                    childrenLists.add(keyValueBean1);
                }
                mChildrenListLists.add(childrenLists);
            }*/

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readStream(InputStream is) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            int i = is.read();
            while (i != -1) {
                bo.write(i);
                i = is.read();
            }
            return bo.toString();
        } catch (IOException e) {
            return "";
        }
    }


    @Override
    public void toButtonClick(String position) {
        Toast.makeText(getActivity(),"---",Toast.LENGTH_LONG).show();
    }
}

